package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.dialog.SingleBtnDialog;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;
import com.taptech.doufu.ui.view.theme.ThemeType;
import com.taptech.doufu.ui.view.theme.read.ReadThemeHelper;

/**
 * Created by op0509 on 2016/5/18.
 */
public class NovelCreatSecondActivity extends DiaobaoBaseActivity implements HttpResponseListener, View.OnClickListener {
    private TextView tvSure,tvCurrCount;
    private EditText etDesc,etRecommendDesc;

    private WaitDialog dialog;

    // 数据
    private String novelId;
    private String novelName;
    private int preCollectCount;

    private ImageView ivPreCollect,ivHelp;
    private LinearLayout llPreCollect;
    private TextView tvTips;
    private boolean isSelect = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_novel_create_second);
        initData();
        initUI();
    }

    private void initUI() {
        tvSure = (TextView) findViewById(R.id.tv_novel_create_second_sure);
        tvCurrCount = (TextView) findViewById(R.id.tvCurrCount);
        tvSure.setOnClickListener(this);

        etDesc = (EditText) findViewById(R.id.et_novel_create_second_desc);
        etRecommendDesc = (EditText) findViewById(R.id.etRecommendDesc);

        ivHelp = (ImageView) findViewById(R.id.ivHelp);
        llPreCollect = (LinearLayout) findViewById(R.id.llPreCollect);
        tvTips = (TextView) findViewById(R.id.tvTips);
        tvTips.setVisibility(View.GONE);
        ivPreCollect = (ImageView) findViewById(R.id.ivPreCollect);
        ivPreCollect.setOnClickListener(this);
        if (preCollectCount > 0) {
            //可以设置预收藏
            llPreCollect.setVisibility(View.VISIBLE);
        }else{
            llPreCollect.setVisibility(View.INVISIBLE);
        }
        ivHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SingleBtnDialog singleBtnDialog = new SingleBtnDialog(NovelCreatSecondActivity.this, R.style.updateDialog,
                        "作品的“预收藏”功能类似于作品的预发布功能，作者可以先创作文章名、简介等信息，暂不发表正文，读者可收藏、分享；“预收藏”功能是作者预热作品的小工具，具体有以下规则：\n" +
                                "1、作者在新建作品时可以设置自己的作品为“预收藏”作品；\n" +
                                "2、每个作者最多只能同时设置2部作品为“预收藏”状态；\n" +
                                "3、设置了“预收藏”的作品将在作者的个人主页默认置顶；\n" +
                                "4、作品任意一章点击”发布“后，作品的预收藏状态将自动取消；\n" +
                                "5、如作品已发布或任意章节、不管章节是否通过，都不能设置预收藏。", "关于“预收藏”功能","好的");
                singleBtnDialog.show();
            }
        });
        etDesc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvCurrCount.setText(String.valueOf(s.length()));
                if (s.length() > 500) {
                    tvCurrCount.setTextColor(Color.parseColor("#fe6e6e"));
                }else{
                    tvCurrCount.setTextColor(Color.parseColor("#999999"));

                }
                updateRightBtn();
            }
        });
    }

    private void initData() {
        // 获取小说id
        novelId = getIntent().getStringExtra(Constant.NOVEL_ID);
        novelName = getIntent().getStringExtra(Constant.NOVEL_NAME);
        preCollectCount = getIntent().getIntExtra(Constant.PRE_COLLECT_COUNT, 0);
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if (dialog != null && dialog.isShowing())
        {
            dialog.dismiss();
        }
        if (response.getStatus() == 0 && response.getFail_code() == 0)
        {
            UIUtil.toastMessage(this, "小说设置成功");
            Intent intent = new Intent(this, NewNovelCreateListTosActivity.class);
            intent.putExtra(Constant.NOVEL_ID, novelId);
            intent.putExtra(Constant.NOVEL_NAME, novelName);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_novel_create_second_sure:
                String sure = tvSure.getText().toString();
                if (isSelect) {
                    if(DiaobaoUtil.getStringSizeNoSpace(etDesc.getText().toString()) > 500){
                        UIUtil.toastMessage(this, "简介字数不能超过500字");
                        return;
                    }
                    if(DiaobaoUtil.getStringSizeNoSpace(etDesc.getText().toString()) < 100){
                        UIUtil.toastMessage(this, "当前作品已设置为预收藏，简介要求100字以上");
                        return;
                    }
                    dialog = new WaitDialog(this, R.style.updateDialog);
                    dialog.show();
                    NovelCreateServices.getInstance().saveTopicNovel(this, novelId, null, etDesc.getText().toString(), "",etRecommendDesc.getText().toString(),1);

                }else{
                    if (etDesc.getText().length() == 0 && etRecommendDesc.getText().length() == 0) {
                        Intent intent = new Intent(this, NewNovelCreateListTosActivity.class);
                        intent.putExtra(Constant.NOVEL_ID, novelId);
                        intent.putExtra(Constant.NOVEL_NAME, novelName);
                        startActivity(intent);
                        finish();
                    }else{
                        if(DiaobaoUtil.getStringSizeNoSpace(etDesc.getText().toString()) > 500){
                            UIUtil.toastMessage(this, "简介字数不能超过500字");
                            return;
                        }
                        dialog = new WaitDialog(this, R.style.updateDialog);
                        dialog.show();
                        NovelCreateServices.getInstance().saveTopicNovel(this, novelId, null, etDesc.getText().toString(), "",etRecommendDesc.getText().toString(),0);
                    }
                }
                break;
            case R.id.ivPreCollect:
                isSelect = !isSelect;
                setPreCollectUI();
                break;
            default:
                break;
        }
    }

    private void setPreCollectUI() {
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            //夜间
            if (isSelect) {
                ivPreCollect.setImageResource(R.drawable.read_switch_o_night);
            }else{
                ivPreCollect.setImageResource(R.drawable.read_switch_c_night);
            }
        }else{
            if (isSelect) {
                ivPreCollect.setImageResource(R.drawable.read_switch_o);
            }else{
                ivPreCollect.setImageResource(R.drawable.read_switch_c);
            }
        }
        if (isSelect){
            tvTips.setVisibility(View.VISIBLE);
        }else {
            tvTips.setVisibility(View.INVISIBLE);
        }
        updateRightBtn();
    }


    private void updateRightBtn() {
        if (isSelect) {
            if (etDesc.getText().length() >= 100) {
                tvSure.setTextColor(ContextCompat.getColor(this,R.color.text_color_57));
            }else{
                tvSure.setTextColor(ContextCompat.getColor(this,R.color.text_color_7));
            }
        }else{
            tvSure.setTextColor(ContextCompat.getColor(this,R.color.text_color_57));
        }
    }

    public void back(View view)
    {
        finish();
    }

}
